package atessitura;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class GuitarNote {
	private int string;
	private int fret;
	private int duration;
	private int startTime;
	private int [] tuning;
	
	private boolean isCopy;
	
	public GuitarNote(int str, int frt, int dur, int start, int [] tune)
	{
		string=str;
		fret=frt;
		duration=dur;
		startTime=start;
		tuning=tune;
		isCopy=false;
	}
	
	public GuitarNote(int str, int frt, int dur, int start, int [] tune, boolean throwsMovementErrors)
	{
		this(str,frt,dur,start,tune);
		isCopy=!throwsMovementErrors;
	}
	
	public GuitarNote(GuitarNote copy)
	{
		this(copy.getString(), copy.getFret(), copy.getDuration(), copy.getStartTime(), copy.getTuning());
		isCopy=true;
	}
	
	public int getString()
	{
		return string;
	}
	
	public int getFret()
	{
		return fret;
	}
	
	public int getDuration()
	{
		return duration;
	}
	
	public int getStartTime()
	{
		return startTime;
	}
	
	public int getEndTime()
	{
		return startTime+duration;
	}
	
	public int getNoteNumber()
	{
		return tuning[getString()]+getFret();
	}
	
	/*public String toString()
	{
		return "GuitarNote string="+getString()+" fret="+getFret()+" start="+getStartTime()+" duration="+getDuration() + " tuning="+arrayToString(tuning);
	}*/
	
	public String toString() // used in file writing
	{
		return getString()+" "+getFret()+" "+getStartTime()+" "+getDuration() + " "+arrayToString(tuning);

	}
	
	public String arrayToString(int[] array)
	{
		String output="";
		for (int i=0;i<array.length-1;i++)
		{
			output+=array[i]+" ";
		}
		output+=array[array.length-1];
		return output;
	}
	
	public int [] getTuning()
	{
		return tuning;
	}
	
	public void setString (int str)
	{
		string = str;
	}
	public void setFret (int frt)
	{
		fret = frt;
	}
	
	public boolean shiftToString(int newStr)
	{
		if (newStr<0 ||newStr>5)
		{
			return false;
		}
		int newFret=getNoteNumber()-tuning[newStr];
		if (newFret<0 || newFret>Guitar.NUM_FRETS)
		{
			if (isCopy)
			{
				return false;
			}
			JOptionPane.showMessageDialog(new JFrame(),
				    "Note is not playable on this string.",
				    "Movement Error",
				    JOptionPane.ERROR_MESSAGE);
			return false;
		}
		setString(newStr);
		setFret(newFret);
		return true;
	}
	
	public boolean equals(Object o)
	{
		if(o instanceof GuitarNote)
		{
			GuitarNote other=(GuitarNote)o;
			if(getString() == other.getString() && getFret()==other.getFret() && getDuration()==other.getDuration() && getStartTime()==other.getStartTime() && getTuning()==this.getTuning()) 
			{
				return true;
			}
		}
		return false;
	}
	
	
}
